<!--
文件名：ParentStudentManagement.vue
作者：教务系统开发团队
创建日期：2025-04-27
描述：家长学生管理组件，实现学生关联功能
-->
<template>
  <div class="parent-student-management">
    <el-card>
      <div slot="header" class="clearfix">
        <span>关联学生管理</span>
      </div>
      
      <el-table :data="studentList" style="width: 100%" v-if="studentList.length > 0">
        <el-table-column prop="student_id" label="学号" width="120"></el-table-column>
        <el-table-column prop="name" label="姓名" width="120"></el-table-column>
        <el-table-column prop="class_info" label="班级"></el-table-column>
      </el-table>
      
      <el-empty description="暂无关联学生" v-else></el-empty>
      
      <div class="add-student-form">
        <el-form :model="addForm" label-width="80px">
          <el-form-item label="学号">
            <el-input v-model="addForm.student_id" placeholder="请输入学生学号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd">添加学生</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getParentStudents, addStudent, removeStudent } from '@/api/parent';

export default {
  name: 'ParentStudentManagement',
  data() {
    return {
      studentList: [],
      addDialogVisible: false,
      addForm: {
        student_id: ''
      }
    };
  },
  created() {
    this.fetchStudentList();
  },
  methods: {
    // 获取学生列表
    async fetchStudentList() {
      try {
        const res = await getParentStudents();
        this.studentList = res.data;
      } catch (error) {
        this.$message.error('获取学生列表失败');
      }
    },
    
    // 显示添加对话框
    showAddDialog() {
      this.addDialogVisible = true;
      this.addForm.student_id = '';
    },
    
    // 处理添加学生
    async handleAdd() {
      try {
        // 先验证学生是否存在
        const checkRes = await this.$http.get(`/api/students/${this.addForm.student_id}/check`);
        
        // 验证手机号是否匹配
        if (checkRes.data.contact_phone !== this.$store.state.user.phone) {
          this.$message.error('手机号码与学生联系人手机号不匹配');
          return;
        }
        
        await addStudent(this.addForm);
        this.$message.success('添加学生成功');
        this.addDialogVisible = false;
        this.fetchStudentList();
      } catch (error) {
        if (error.response && error.response.status === 404) {
          this.$message.error('未找到该学生，请确认学号是否正确');
        } else if (error.response && error.response.status === 400) {
          this.$message.error(error.response.data.detail || '手机号码与学生联系人手机号不匹配');
        } else {
          this.$message.error(error.message || '添加学生失败');
        }
      }
    },
    
    // 处理移除学生
    async handleRemove(student) {
      try {
        await this.$confirm(`确定要移除学生 ${student.name} 吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        
        await removeStudent({ student_id: student.id });
        this.$message.success('移除学生成功');
        this.fetchStudentList();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error(error.message || '移除学生失败');
        }
      }
    }
  }
};
</script>

<style scoped>
.parent-student-management {
  padding: 20px;
}
</style>